package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static java.lang.System.exit;

public class BibliotecaApp {

    public List<Book> bookList = new ArrayList<Book>();

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

    public void greet() {
        System.out.println("Welcome to Biblioteca !!");
    }

    public void addBook(String ISBN, String name, String author) {
        bookList.add(new Book(ISBN, name, author));
    }

    public void populateBooks() {
        addBook("A14171", "Harry Potter and the Chamber of Secrets", "Rowling JK");
        addBook("H76526", "Angels and Demons", "Dan Brown");
    }

    public void displayMenuOptions() {
        System.out.println("1. List Books");
        System.out.println("2. Reserve a Book");
        System.out.println("3. Check Membership Details");
        System.out.println("0. Exit");
    }

    public int readMenuOption() throws IOException {
        System.out.print("\nPlease Enter Your Choice ..");
        int menuOption = Integer.parseInt(bufferedReader.readLine());
        return menuOption;
    }

    public boolean validateMenuOption(int menuOption) {
        if (menuOption != 0 || menuOption != 1 || menuOption != 2 || menuOption != 3 )
            return true;
        return false;
    }

    public void listBooks() {
        for(int i = 0; i < bookList.size(); i++) {
            System.out.print("ISBN: " + bookList.get(i).getISBN() + "  ");
            System.out.print("Name: " + bookList.get(i).getName() + "  ");
            System.out.print("Author: " + bookList.get(i).getAuthor() + "  ");
            System.out.print("Availability: " + bookList.get(i).getAvailability() + "\n");
        }
    }

    public void reserveBook() throws IOException {
        boolean proceed = true;
        while(proceed) {
            System.out.print("\nPlease Enter the ISBN number of the book you wish to borrow: ");
            String ISBN = bufferedReader.readLine();
            Book book = searchBook(ISBN);
            if (bookList.contains(book) ) {
                if(book.getAvailability()) {
                    System.out.println("Thank You! Enjoy the book.");
                    book.setAvailability(false);
                }
                else {
                    System.out.print("Sorry we don't have that book yet.");
                }
                proceed = false;
            }
            else {
                System.out.print("Book Not Found. Enter the Correct ISBN Number !!");
            }
        }
    }

    public  Book searchBook (String ISBN) {
        for (Book book : bookList) {
            if (book.getISBN().equals(ISBN)) {
                return book;
            }
        }
        return null;
    }
    public void checkMemberShipDetails () {
        System.out.println("Please talk to a Librarian. Thank you.");

    }

    public static void main(String[] args) throws IOException {
        BibliotecaApp bibliotecaApp = new BibliotecaApp();
        bibliotecaApp.greet();
        bibliotecaApp.populateBooks();
        bibliotecaApp.displayMenuOptions();
        int selectedOption = bibliotecaApp.readMenuOption();
        while (selectedOption != 0) {
            boolean isValidMenuOption = bibliotecaApp.validateMenuOption(selectedOption);
            if(isValidMenuOption) {
                switch (selectedOption) {
                    case 1:
                        bibliotecaApp.listBooks();
                        break;
                    case 2:
                        bibliotecaApp.reserveBook();
                        break;
                    case 3:
                        bibliotecaApp.checkMemberShipDetails();
                        break;
                }
            }
            else {
                System.out.print("Select a valid option!!");
            }
            selectedOption = bibliotecaApp.readMenuOption();
        }
        exit(0);
    }
}
